package com.semanticweb.webapp.model.ontology;

import org.apache.lucene.document.Document;

import com.semanticweb.webapp.ontology.CIDOC;
import com.semanticweb.webapp.ontology.CType;
import com.semanticweb.webapp.ontology.Ont;
import com.semanticweb.webapp.ontology.Query;
import com.semanticweb.webapp.search.IDocument;
import com.semanticweb.webapp.search.SearchMD;

/**
 * Class which represents 'Literatura' from the chart. * * <br>
 * value parameter represents name of literature
 * 
 * @author Lab
 * 
 */
public class Literature extends DefaultAbstractEntity<String> implements IDocument {

    public static final String ONT_LITERATURE = "literature";
    public static final String ONT_DOCUMENT = "document";
    public static final String ONT_APPELLATION = "appellation";
    public static final String ONT_IDENTIFIER = "identifier";

    private String refId;

    public Literature(final String id) {
        super(id);
    }

    @Override
    public void toRDF() {
        if (this.value == null && this.refId == null) {
            return;
        }

        Ont.typeCheck(CType.LITERATURE);

        final String itemURI = Item.formatItemURI(this.id);
        final Query q = new Query();
        String[] result = null;

        if (this.value != null && this.refId != null) {
            result = q.select("document").type("document", CIDOC.E31.queryValue())
                    .type("appellation", CIDOC.E41.queryValue()).label("appellation", this.value)
                    .type("identifier", CIDOC.E42.queryValue()).label("identifier", this.refId)
                    .type("type", CIDOC.E55.queryValue()).connect("document", CIDOC.P2.queryValue(), "type")
                    .connect("document", CIDOC.P1.queryValue(), "appellation")
                    .connect("document", CIDOC.P48.queryValue(), "identifier").find();
        }

        String docURI;
        if (result == null || result.length == 0) {
            docURI = Ont.formatURI(this.id, ONT_LITERATURE, ONT_DOCUMENT);
            final String appellationURI = Ont.formatURI(this.id, ONT_LITERATURE, ONT_APPELLATION);
            final String identifierURI = Ont.formatURI(this.id, ONT_LITERATURE, ONT_IDENTIFIER);

            Ont.getOntology().createIndividual(CIDOC.E31.getFullValue(), docURI);
            Ont.getOntology().connectIndividuals(docURI, CIDOC.P2.getFullValue(), CType.LITERATURE.getURI());

            // name
            if (this.value != null) {
                Ont.getOntology().createIndividual(CIDOC.E41.getFullValue(), appellationURI);
                Ont.getOntology().addLabel(appellationURI, this.value);
                Ont.getOntology().connectIndividuals(docURI, CIDOC.P1.getFullValue(), appellationURI);
            }

            // refId
            if (this.refId != null) {
                Ont.getOntology().createIndividual(CIDOC.E42.getFullValue(), identifierURI);
                Ont.getOntology().addLabel(identifierURI, this.refId);
                Ont.getOntology().connectIndividuals(docURI, CIDOC.P48.getFullValue(), identifierURI);
            }
        } else {
            docURI = result[0];
        }

        // connect to item
        Ont.getOntology().connectIndividuals(docURI, CIDOC.P67.getFullValue(), itemURI);
    }

    public void fromRDF() {
        final String itemQueryValue = Item.formatItemQueryURI(this.id);
        final Query query = new Query();

        // name of literature
        String[] result = query.selectLabel().type("doc", CIDOC.E31.queryValue())
                .connect("doc", CIDOC.P67.queryValue(), itemQueryValue)
                .connect("doc", CIDOC.P2.queryValue(), CType.LITERATURE.queryValue())
                .type("name", CIDOC.E41.queryValue()).connect("doc", CIDOC.P1.queryValue(), "name").findLabel("name");
        if (result != null && result.length > 0) {
            this.value = result[0];
        }

        // ref id
        result = query.selectLabel().type("doc", CIDOC.E31.queryValue())
                .connect("doc", CIDOC.P67.queryValue(), itemQueryValue)
                .connect("doc", CIDOC.P2.queryValue(), CType.LITERATURE.queryValue())
                .type("id", CIDOC.E42.queryValue()).connect("doc", CIDOC.P48.queryValue(), "id").findLabel("id");
        if (result != null && result.length > 0) {
            this.refId = result[0];
        }
    }

    public String getRefId() {
        return this.refId;
    }

    public void setRefId(final String refId) {
        this.refId = refId;
    }

    @Override
    public void toIndexDocument(final Document doc) {
        if (this.value != null) {
            doc.add(SearchMD.CONTENT.createField(this.value));
        }
    }

}
